package persistence;

import java.io.*;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import model.Producto;

public class ProductoDAOFileImplementation implements ProductoDAO {

    private Map<String, Producto> productMap = new HashMap<String, Producto>();
    private String fileName;
    private static ProductoDAOFileImplementation persistenceManager = null;

    private ProductoDAOFileImplementation() {
    }

    public static ProductoDAO getJDBCPersistenceManager() {
        if (persistenceManager == null) {
            persistenceManager = new ProductoDAOFileImplementation();
        }
        return persistenceManager;
    }

    @Override
    public boolean setUp(String url, String driver, String user, String password) {
        this.fileName = url;
        try {
            File file = new File(fileName);
            if (!file.exists()) {
                return true;
            }
            InputStream inputStream = new FileInputStream(file);
            ObjectInputStream objectInputStream = new ObjectInputStream(inputStream);
            int numberOfProducts = (Integer) objectInputStream.readObject();
            for (int i = 0; i < numberOfProducts; i++) {
                Producto product = (Producto) objectInputStream.readObject();
                getProductMap().put(product.getNombre(), product);
            }
            objectInputStream.close();
        } catch (Exception ex) {
            Logger.getLogger(ProductoDAOFileImplementation.class.getName()).log(Level.SEVERE,
                    "No se pudieron cargar los datos del archivo", ex);
            return false;
        }
        return true;
    }

    @Override
    public boolean disconnect() {
        try {
            OutputStream outputStream = new FileOutputStream(new File(fileName));
            ObjectOutputStream objectOutputStream = new ObjectOutputStream(outputStream);
            objectOutputStream.writeObject(getProductMap().size());
            for (Producto product : getProductMap().values()) {
                objectOutputStream.writeObject(product);
            }
            objectOutputStream.close();
        } catch (IOException ex) {
            Logger.getLogger(ProductoDAOFileImplementation.class.getName()).log(Level.SEVERE,
                    "No se pudieron guardar los datos en el archivo", ex);
            return false;
        }
        return true;
    }

    @Override
    public synchronized boolean newProduct (Producto product) {
        if (getProductMap().containsKey(product.getNombre())) {
            return false;
        } else {
            getProductMap().put(product.getNombre(), product);
            return true;
        }
    }

    @Override
    public synchronized boolean deleteProduct(String name) {
        if (!productMap.containsKey(name)) {
            return false;
        } else {
            getProductMap().remove(name);
            return true;
        }
    }

    @Override
    public synchronized boolean updateProduct(String name, Producto producto) {
        if (!productMap.containsKey(name)) {
            return false;
        } else {
            getProductMap().put(producto.getNombre(), producto);
            return true;
        }
    }

    @Override
    public synchronized Producto readProduct(String name) {
        return getProductMap().get(name);
    }

    /**
     * @return the productMap
     */
    public Map<String, Producto> getProductMap() {
        return productMap;
    }


    
}
